<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="//g.alicdn.com/msui/sm/0.6.2/css/sm.min.css">
  <link rel="stylesheet" href="//g.alicdn.com/msui/sm/0.6.2/css/sm-extend.min.css">
  <link rel="stylesheet" href="./css/index.css">
  <title>欢迎</title>
</head>

<body>
  <div class="content" id="content-box" style="display:none">

    <div class="list-block">
      <ul>
        <!-- Text inputs -->
        <li>
          <div class="item-content">
            <div class="item-media">
              <i class="icon icon-form-phone"></i>
            </div>
            <div class="item-inner">
              <div class="item-title label">手机号码</div>
              <div class="item-input phone">
                <input type="text" placeholder="输入手机号码" onblur="index.changeInput('register','phone')">
              </div>
            </div>
          </div>
        </li>
        <li>
          <div class="item-content">
            <div class="item-media">
              <i class="icon icon-form-code"></i>
            </div>
            <div class="item-inner">
              <div class="item-title label">验证码</div>
              <div class="item-input verificationCode">
                <input type="text" placeholder="输入验证码" onblur="index.changeInput('register','verificationCode')">
              </div>
              <a href="#" class="button button-big button-fill button-success" id="getCodeBtn" onclick="index.http('getCode')">获取验证码</a>
            </div>
          </div>
        </li>
        <li>
          <div class="item-content">
            <div class="item-media">
              <i class="icon icon-form-gift"></i>
            </div>
            <div class="item-inner">
              <div class="item-title label">推荐码</div>
              <div class="item-input recommendCode">
                <input type="text" placeholder="输入推荐码" onblur="index.changeInput('register','recommendCode')">
              </div>
            </div>
          </div>
        </li>
      </ul>
    </div>
    <div class="content-block">
      <div class="row">
        <div class="col-100">
          <a href="#" class="button button-big button-fill button-success" onclick="index.http('register')">注册绑定</a>
        </div>
      </div>
    </div>
  </div>

</body>
<script type='text/javascript' src='//g.alicdn.com/sj/lib/zepto/zepto.min.js' charset='utf-8'></script>
<script type="text/javascript" src="./js/index.js"></script>
<script>
  var index = (function ($) {
    var index = new ST({
      openid: '',
      checkBd: {
        channelId: ''
      },
      register: {
        phone: '',
        verificationCode: '111111',
        password: '111111',
        recommendCode: '',
        channelId: '',
        name: '',
        headImg: ''
      },
      getUserInfo: {
        accesstoken: '',
        openid: ''
      },
      getCodeTime: null
    })
    index.pushAjax({
      getOpenId: {
        url: '/app/api/public/wxServlet/getOpenId',
        data: {
          code: index.getLocationSearch('code')
        }
      },
      checkBd: {
        url: '/app/api/public/user/checkUserByWX',
        data: index.checkBd
        // data:{channelId:'oEgpiuK6Tp9pEiW_a8c3Ed2yqqqI'}
      },
      register: {
        url: '/app/api/public/user/registerByWX',
        data: index.register
      },
      getUserInfo: {
        url: '/app/api/public/wxServlet/getUserInfo',
        data: index.getUserInfo
      },
      getCode: {
        url: '/app/api/public/sms/sendSms',
        data: index.register
      }
    })

    index.pushAjaxSuccess({
      getOpenId: function (res) {
        if (res.code == 1) {
          index.checkBd.channelId = res.data.openid
          index.register.channelId = res.data.openid
          index.getUserInfo.accesstoken = res.data.access_token
          index.getUserInfo.openid = res.data.openid
          index.http('checkBd')
        } else {}
      },
      checkBd: function (res) {
        if (res.code == 1 && res.data.isHave == 1) {
          index.getLocationSearch('state') !== 'home' 
            ? window.location.href = './html/' + index.getLocationSearch('state') + '.html'
            : window.location.href = 'http://shengshouhui.com/wap.html'
        } else {
          index.http('getUserInfo')
        }
      },
      register: function (res) {
        if (res.code == 1) {
          window.location.href = './html/' + index.getLocationSearch('state') + '.html'
        } else {
          alert(res.msg)
        }
      },
      getUserInfo: function (res) {
        index.register.name = res.data.nickname
        index.register.headImg = res.data.headimgurl
        $('#content-box').show()
        document.title = '注册绑定'
      },
      getCode: function (res) {
        if (index.getCodeTime) {
          clearInterval(index.getCodeTime)
        }
        if (res.code == 1) {
          $('#getCodeBtn').removeClass('button-success').addClass('disabled').text('60s')
          var second = 60
          $('#getCodeBtn')[0].onclick = null
          index.getCodeTime = setInterval(function () {
            if (second == 0) {
              $('#getCodeBtn')[0].onclick = function () {
                index.http('getCode')
              }
              $('#getCodeBtn').removeClass('disabled').addClass('button-success').text("获取验证码")
              second = 60
              clearInterval(index.getCodeTime)
              return
            }
            second -= 1
            $('#getCodeBtn').text(second + 's')
          }, 1e3)
        }
      }
    })

    if(sessionStorage.getItem('isLogin')) {
      window.location.href = './html/'+sessionStorage.getItem('isLogin') + '.html'
    }else{
      index.http('getOpenId')
    }
    index.render()
    return index
  })($)
</script>

</html>